package cn.ibizlab.odoo.core.odoo_fetchmail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ifetchmail_server;
import cn.ibizlab.odoo.core.odoo_fetchmail.domain.Fetchmail_server;
import cn.ibizlab.odoo.core.odoo_fetchmail.filter.Fetchmail_serverSearchContext;
import cn.ibizlab.odoo.core.odoo_fetchmail.service.IFetchmail_serverService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[Incoming Mail Server] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Fetchmail_serverServiceImpl implements IFetchmail_serverService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ifetchmail_serverClientService fetchmail_serverClientService;

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //fetchmail_serverClientService.removeBatch(fetchmail_servers) ;

    }

    @Override
    public void createBatch(List<Fetchmail_server> fetchmail_servers, int batchSize) {

                        //fetchmail_serverClientService.createBatch(fetchmail_servers,batchSize) ;

    }

    @Override
    public Fetchmail_server get(Integer id) {
        Ifetchmail_server clientModel = fetchmail_serverClientService.createModel();
        clientModel.setId(id);
        fetchmail_serverClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Fetchmail_server get(Fetchmail_server fetchmail_server) {
        Fetchmail_server retfetchmail_server = this.get(fetchmail_server.getId()) ;
    	BeanCopier copier=BeanCopier.create(retfetchmail_server.getClass(), fetchmail_server.getClass(), false);
        copier.copy(retfetchmail_server, fetchmail_server, null);
        return fetchmail_server ;
    }

    @Override
    public boolean update(Fetchmail_server fetchmail_server) {
        boolean bOk = false ;
        Ifetchmail_server clientModel = this.convert2Model(fetchmail_server,null);
        fetchmail_serverClientService.update(clientModel) ;
        this.convert2Domain(clientModel,fetchmail_server) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean create(Fetchmail_server fetchmail_server) {
        boolean bOk = false ;
        Ifetchmail_server clientModel = this.convert2Model(fetchmail_server,null);
        fetchmail_serverClientService.create(clientModel) ;
        this.convert2Domain(clientModel,fetchmail_server);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ifetchmail_server clientModel = fetchmail_serverClientService.createModel();
        clientModel.setId(id);
        fetchmail_serverClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Fetchmail_server> fetchmail_servers, int batchSize) {

        //fetchmail_serverClientService.updateBatch(fetchmail_servers,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Fetchmail_server> searchDefault(Fetchmail_serverSearchContext context) {
        List<Fetchmail_server> list = new ArrayList<Fetchmail_server>();
        Page<cn.ibizlab.odoo.core.client.model.Ifetchmail_server> clientModelList = fetchmail_serverClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ifetchmail_server clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Fetchmail_server>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据用户查询
     */
    @Override
    public Page<Fetchmail_server> selectByOdoo_create(Integer id){
        Fetchmail_serverSearchContext context = new Fetchmail_serverSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Fetchmail_server> list = new ArrayList<Fetchmail_server>();
        Page<cn.ibizlab.odoo.core.client.model.Ifetchmail_server> clientModelList = fetchmail_serverClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ifetchmail_server clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Fetchmail_server>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Fetchmail_server> selectByOdoo_write(Integer id){
        Fetchmail_serverSearchContext context = new Fetchmail_serverSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Fetchmail_server> list = new ArrayList<Fetchmail_server>();
        Page<cn.ibizlab.odoo.core.client.model.Ifetchmail_server> clientModelList = fetchmail_serverClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ifetchmail_server clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Fetchmail_server>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ifetchmail_server convert2Model(Fetchmail_server domain , Ifetchmail_server model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = fetchmail_serverClientService.createModel() ;
        }

         if(domain.getUserDirtyFlag())
            model.setUser(domain.getUser());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getScriptDirtyFlag())
            model.setScript(domain.getScript());
         if(domain.getConfigurationDirtyFlag())
            model.setConfiguration(domain.getConfiguration());
         if(domain.getDateDirtyFlag())
            model.setDate(domain.getDate());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.getPasswordDirtyFlag())
            model.setPassword(domain.getPassword());
         if(domain.getMessage_idsDirtyFlag())
            model.setMessage_ids(domain.getMessage_ids());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getServerDirtyFlag())
            model.setServer(domain.getServer());
         if(domain.getPortDirtyFlag())
            model.setPort(domain.getPort());
         if(domain.getIs_sslDirtyFlag())
            model.setIs_ssl(domain.getIs_ssl());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getPriorityDirtyFlag())
            model.setPriority(domain.getPriority());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getObject_idDirtyFlag())
            model.setObject_id(domain.getObject_id());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getAttachDirtyFlag())
            model.setAttach(domain.getAttach());
         if(domain.getOriginalDirtyFlag())
            model.setOriginal(domain.getOriginal());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Fetchmail_server convert2Domain( Ifetchmail_server model ,Fetchmail_server domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Fetchmail_server();
        }

        if(model.getUserDirtyFlag())
            domain.setUser(model.getUser());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getScriptDirtyFlag())
            domain.setScript(model.getScript());
        if(model.getConfigurationDirtyFlag())
            domain.setConfiguration(model.getConfiguration());
        if(model.getDateDirtyFlag())
            domain.setDate(model.getDate());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.getPasswordDirtyFlag())
            domain.setPassword(model.getPassword());
        if(model.getMessage_idsDirtyFlag())
            domain.setMessage_ids(model.getMessage_ids());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getServerDirtyFlag())
            domain.setServer(model.getServer());
        if(model.getPortDirtyFlag())
            domain.setPort(model.getPort());
        if(model.getIs_sslDirtyFlag())
            domain.setIs_ssl(model.getIs_ssl());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getPriorityDirtyFlag())
            domain.setPriority(model.getPriority());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getObject_idDirtyFlag())
            domain.setObject_id(model.getObject_id());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getAttachDirtyFlag())
            domain.setAttach(model.getAttach());
        if(model.getOriginalDirtyFlag())
            domain.setOriginal(model.getOriginal());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
